package com.example.woke1;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.fragment.DingqiFragment;
import com.woke.fragment.HuoqiFragment;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class MylicaiActivity extends FragmentActivity implements
		OnClickListener, OnCheckedChangeListener {

	private FragmentTabHost mTabHost;
	private RadioGroup radioGroup;
	private MyApp application;
	private RadioButton mRiobhuoqi;
	private RadioButton mRiobdingqi;
	private TextView mTtotal;
	private TextView mThuoording;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mylicai);

		intview();

	}

	private void intview() {
		application = (MyApp) getApplication();
		findViewById(R.id.avmylicai_image_back).setOnClickListener(this);
		mThuoording = (TextView) findViewById(R.id.avlicai_text_huoording);
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setHorizontalScrollBarEnabled(false);
		mTabHost.setVerticalScrollBarEnabled(false);
		mTabHost.setup(MylicaiActivity.this, getSupportFragmentManager(),
				R.id.avrealtabcontent);
		mTabHost.getTabWidget().setVisibility(View.GONE);

		mTabHost.addTab(
				mTabHost.newTabSpec("avindex3").setIndicator("avHome3"),
				HuoqiFragment.class, null);
		mTabHost.addTab(
				mTabHost.newTabSpec("avmonitor3").setIndicator("avMessage3"),
				DingqiFragment.class, null);
		mTabHost.setCurrentTabByTag("avindex3");
		mTtotal = (TextView) findViewById(R.id.avmylicai_text_money);// ���ʲ�
		mRiobhuoqi = (RadioButton) findViewById(R.id.avradio_index3);
		mRiobhuoqi.setChecked(true);
		mRiobdingqi = (RadioButton) findViewById(R.id.avradio_monotor3);
//		mRiobdingqi.setChecked(true);
		radioGroup = (RadioGroup) findViewById(R.id.avradiogroup3);
		radioGroup.setOnCheckedChangeListener(this);
		getmylicaiinfo();
	}

	private void getmylicaiinfo() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				MylicaiActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "myFinancial");
		params.put("userid", application.getDatas_load().getId());
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				// freeze_money���ʲ�money���ڽ��d_money���ڽ��
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("success")) {
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jso = jsonObject.getJSONObject("data");
						String freeze_money = ""
								+ jso.getString("freeze_money");
						String money = "" + jso.getString("money");
						String d_money = "" + jso.getString("d_money");
						if (freeze_money.equals("") || freeze_money.equals("0")) {
							mTtotal.setText("￥"+ "0.00元");
						} else {
							String freeze_money1 = freeze_money.substring(0,
									freeze_money.length() - 3);
							mTtotal.setText("￥" + freeze_money1 + "元");

						}
						if (money.equals("") || money.equals("0")) {
							mRiobhuoqi.setText("活期： " + "0.00元");
						} else {
							String money1 = money.substring(0, money.length() - 3);
							mRiobhuoqi.setText("活期： " + money1 + "元");

						}
						if (d_money.equals("") || d_money.equals("0")) {
							mRiobdingqi.setText("定期： "+ "0.00元");
						} else {
							String d_money1 = d_money.substring(0,
									d_money.length() - 3);
							mRiobdingqi.setText("定期： " + d_money1 + "元");

						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				// Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				// mProgress.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				// mProgress.setVisibility(View.GONE);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.avmylicai_image_back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		FragmentManager fm = getSupportFragmentManager();
		HuoqiFragment avindex3 = (HuoqiFragment) fm
				.findFragmentByTag("avindex3");
		DingqiFragment avmonitor3 = (DingqiFragment) fm
				.findFragmentByTag("avmonitor3");
		FragmentTransaction ft = fm.beginTransaction();
		// ** Detaches the androidfragment if exists */
		if (avindex3 != null)
			ft.detach(avindex3);
		if (avmonitor3 != null)
			ft.detach(avmonitor3);
		switch (checkedId) {
		case R.id.avradio_index3:
			mThuoording.setText("账单");
			if (avindex3 == null) {
				ft.add(R.id.avrealtabcontent3, new HuoqiFragment(), "avindex3");
			} else {
				ft.attach(avindex3);
			}
			mTabHost.setCurrentTabByTag("avindex3");
			break;
		case R.id.avradio_monotor3:
			mThuoording.setText("投资记录");
			if (avmonitor3 == null) {
				ft.add(R.id.avrealtabcontent3, new DingqiFragment(),
						"avmonitor3");
			} else {
				ft.attach(avmonitor3);
			}
			mTabHost.setCurrentTabByTag("avmonitor3");
			break;
		}
	}
}
